<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:user:add']">新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:user:edit']">修改</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:user:remove']">删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport"
          v-hasPermi="['system:user:export']">导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="${comment}" align="center" prop="id" v-if="false"/>
      <el-table-column label="所属小区" align="center" prop="estateName" />
      <el-table-column label="人员姓名" align="center" prop="userName" />
      <el-table-column label="人员类型" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.userType == 1" style="cursor: pointer" :type="''">管理员</el-tag>
          <el-tag v-if="scope.row.userType == 2" style="cursor: pointer" :type="''">接单员</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="人员电话" align="center" prop="phoneNum" />
      <el-table-column label="是否启用" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state == 0" style="cursor: pointer" :type="'danger'">禁用</el-tag>
          <el-tag v-if="scope.row.state == 1" style="cursor: pointer" :type="''">可用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="是否为管理员" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isManager == 1" style="cursor: pointer" :type="'success'">管理员</el-tag>
          <el-tag v-if="scope.row.isManager == 0" style="cursor: pointer" :type="''">非管理员</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:user:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:user:remove']">删除</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改商家的管理用户wxuser的用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属小区" prop="estateId" v-if="open2">
          <el-select v-model="form.estateId" placeholder="请选择小区" style="width:180px;" size="mini" filterable clearable>
            <el-option v-for="estate in estateChildOptions"
                       :key="estate.id"
                       :label="estate.estateName"
                       :value="estate.id"/>
          </el-select>
        </el-form-item>

<!--        <el-form-item label="小区Id" prop="estateId">
          <el-input v-model="form.estateId" placeholder="小区Id" size="mini"/>
        </el-form-item>-->
<!--        <el-form-item label="人员id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入人员id" size="mini"/>
        </el-form-item>-->
        <el-form-item label="人员姓名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入人员姓名" size="mini"/>
        </el-form-item>
        <el-form-item label="管理员" prop="isManager">
          <el-radio-group v-model="form.userType" style="width:95%;" size="mini">
            <el-radio :value="1" :label="1">管理员</el-radio>
            <el-radio :value="2" :label="2">接单员</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="人员电话" prop="phoneNum">
          <el-input v-model="form.phoneNum" placeholder="请输入人员电话" size="mini"/>
        </el-form-item>
        <el-form-item label="添加时间" prop="createTime">
          <el-date-picker clearable size="mini" v-model="form.createTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择添加时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否可用" prop="state">
          <el-radio-group v-model="form.state" style="width:95%;" size="mini">
            <el-radio :value="1" :label="1">可用</el-radio>
            <el-radio :value="0" :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="管理员" prop="isManager">
          <el-radio-group v-model="form.isManager" style="width:95%;" size="mini">
            <el-radio :value="1" :label="1">管理员</el-radio>
            <el-radio :value="0" :label="0">非管理员</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <el-form ref="form" label-width="80px">
        <el-form-item label="微信编号" prop="nickName">
          <el-input v-model="form.userNo" placeholder="请输入用户昵称" size="mini"
                    maxlength="30" @onblur="getListMpSearch" clearable>
            <template slot="append">
              <el-button @click="getListMpSearch" size="mini">搜索</el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="微信昵称" prop="userId" v-if="wxUserOptions.length > 0">
          <div class="wx-user">
            <el-radio-group v-model="form.userId">
              <el-row v-for="wxUser in wxUserOptions">
                <el-col :span="24">
                  <el-radio :label="wxUser.id" :value="wxUser.id">
                    <a :href="wxUser.faceUrl" style="color: #42b983" target="_blank">
                      <img :src="wxUser.faceUrl" alt="点击打开" class="el-avatar">
                    </a> {{wxUser.nickName}}({{wxUser.phoneNum!=null?wxUser.phoneNum:''}})
                  </el-radio>
                </el-col>
              </el-row>
            </el-radio-group>
          </div>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser, exportUser } from "@/api/module/property/user";
import {listEstate} from "@/api/module/estate/estate";
import {delMer} from "@/api/module/mer/mer";
import {listByNickName} from "@/api/module/wx/user";

export default {
  name: "User",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      //子分类
      estateChildOptions:[],
      // 总条数
      total: 0,
      // 商家的管理用户wxuser的用户表格数据
      userList: [],
      queryParamsWxUser:{
        nickName:undefined,
        userNo:undefined,
      },
      /*formUserMp:{
        id:undefined,
        estateId: undefined,
        nickName: undefined,
        faceUrl: undefined,
        sysUserId:undefined,
        userId:undefined,
        userType:3
      },*/
      wxUserOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否可用修改小区
      open2: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        estateId: undefined,
        userId: undefined,
        sysUserId:undefined,
        userName: undefined,
        userType: undefined,
        phoneNum: undefined,
        state: undefined,
        isManager: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }

  },
  created() {
    this.getList();
    this.getEstateChildOptionlist();
  },
  methods: {
    /** 查询商家的管理用户wxuser的用户列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getListMpSearch(){
      this.queryParamsWxUser.userNo = this.form.userNo;
      listByNickName(this.queryParamsWxUser).then(response => {
        console.log('getListMpSearch',response.data);
        this.wxUserOptions = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        estateId: undefined,
        userId: undefined,
        userName: undefined,
        userType: undefined,
        phoneNum: undefined,
        createTime: undefined,
        state: undefined,
        isManager: undefined,
      }
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length!==1;
      this.multiple = !selection.length
    },
    getEstateChildOptionlist(){
      listEstate(this.queryParamsPty).then(response => {
        this.estateChildOptions = response.rows;
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.open2 = true;
      this.title = "添加中介人员信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getUser(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改中介人员信息";
      });
    },

    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true;
        return delUser(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateUser(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.open2 = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }else {
            addUser(this.form).then(response => {
              this.msgSuccess("添加成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
  }
}
</script>
